<template>
  <div>
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%" @close="handleClose">
      <!-- 搜索 -->
      <div class="detailed-search">
        <div class="form-item">
          <span>商品名称：</span>
          <el-input v-model="dialogForm.goodsName" placeholder="请输入商品名称" clearable></el-input>
        </div>
        <div class="form-item">
          <span>运营商：</span>
          <el-input v-model="dialogForm.operator" placeholder="请输入运营商" clearable></el-input>
        </div>
        <div class="form-item">
          <span>商品渠道：</span>
          <el-select v-model="dialogForm.supplierShopId" :disabled="displayOwnGoods" clearable placeholder="请选择商品渠道">
            <el-option v-for="item in supplierShopIdOptions" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
          </el-select>
        </div>
        <el-button @click="opretion('cancel')">重置</el-button>
        <el-button style="margin-left:0" type="primary" @click="opretion('search')">搜索</el-button>
      </div>
      <!-- 表格 -->
      <div class="table-box">
        <el-table :max-height="tableHeight" @current-change="handleSelectionChange"
          :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column width="70px" label="选择" align="center">
            <template v-slot="scope">
              <el-radio v-model="tableRadio" :label="scope.$index">&nbsp;</el-radio>
            </template>
          </el-table-column>
          <el-table-column prop="goodsName" label="商品名称" align="center"></el-table-column>
          <el-table-column prop="goodsId" label="商品编码" align="center" width="200"></el-table-column>
          <el-table-column prop="productName" label="号卡产品" align="center"></el-table-column>
          <el-table-column prop="operator" label="运营商" align="center"></el-table-column>
          <el-table-column prop="provinceAreaName" label="归属省市" align="center"></el-table-column>
          <el-table-column prop="supplierShopName" label="商品渠道" align="center" width="180"></el-table-column>
          <el-table-column prop="scopeShops" label="可见分销商" align="center" width="180">
            <template v-slot="scope">
              <div v-for="item in scope.row.scopeShops" :key="item">
                <span>{{item}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="maxGoodsLevel" label="最大分销层级" align="center"></el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { onMounted, reactive, ref, toRefs, watch, computed } from 'vue'
import { getWindowWidthAndHeight, warningDeal, successDeal, getDateTime, BlobDownLoad, numFormat } from '@/utils/utils'
import api from './api'

export default {
  emits: ['addGoods'],
  props: {
    displayOwnGoods: {
      type: Boolean,
      default: false
    }
  },
  setup(props, context) {
    const state = reactive({
      dialogTitle: '选择商品',
      dialogVisible: false,
      dialogForm: {
        supplierShopId: '',
        goodsName: '',
        operator: ''
      },
      page: 1,
      size: 10,
      total: 0,
      tableHeight: 0,
      shopId: '',
      tableData: [],
      supplierShopIdOptions: [],
      tableRadio: '',
      commodityInformation: {}
    })

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 450
    })

    // 供应商=商品渠道
    const getMySupplierList = () => {
      api.mySupplierList().then(res => {
        if (res.code == '200') {
          state.supplierShopIdOptions = res.data
        }
      }).catch(err => {
        console.log(err)
      })
    }

    // 我的商户
    function getShopInfo() {
      api.shopInfo().then(res => {
        if (res.code == '200') {
          if (props.displayOwnGoods) {
            state.dialogForm.supplierShopId = res.data.shopId
          }
          let obj = {
            shopId: res.data.shopId,
            shopName: res.data.shopName
          }
          state.supplierShopIdOptions.push(obj)
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleClose() {
      state.tableRadio = ''
      state.commodityInformation = {}
    }


    function dialogShow() {
      getList()
      getShopInfo()
      getMySupplierList()
      state.dialogVisible = true
    }


    function handleCurrentChange(e) {
      state.page = e
      getList()
    }

    function handleSizeChange(e) {
      state.size = e
      getList()
    }

    const getList = () => {
      let json = {
        page: state.page,
        size: state.size,
        supplierShopId: state.dialogForm.supplierShopId,
        goodsName: state.dialogForm.goodsName,
        operator: state.dialogForm.operator
      }
      api.offlineGoodsSearch(json).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.records
          state.total = res.data.total
        }
      }).catch((err) => {
        console.log(err)
      })
    }

    const handleSelectionChange = (selection) => {
      state.commodityInformation = selection
    }

    const opretion = (v, m) => {
      switch (v) {
        case "cancel":
          state.dialogForm = {
            supplierShopId: '',
            goodsName: '',
            operator: ''
          }
          break;
        case 'search':
          getList()
          break;
        default:
          break;
      }
    }

    // 确定
    const submitHandle = () => {
      if (state.commodityInformation) {
        state.dialogVisible = false
        context.emit("addGoods", state.commodityInformation);
      }
    }

    return {
      ...toRefs(state),
      handleCurrentChange,
      handleSizeChange,
      getList,
      dialogShow,
      handleSelectionChange,
      handleClose,
      submitHandle,
      opretion
    }
  }

}
</script>

<style lang="scss" scoped>
//detailed-search{}
.detailed-search {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 20px;
  box-sizing: border-box;
  padding: 20px 0;
  .form-item {
    width: 25%;
    max-width: 300px;
    display: flex;
    align-items: center;
    gap: 5px;

    > span {
      font-size: 14px;
      flex-shrink: 0;
    }
  }
}
.table-box {
  .table-footer {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>